/**
 * 
 */
package com.star.util.xml;

import java.io.StringReader;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

/**
 * @ClassName: StaxParse
 * @Description: TODO
 * @author starhq
 * @createtime 2014年7月28日 下午4:24:16
 * @email : jian.wang@shinsoft.net
 * @modify by TODO
 * @resaon
 * @edittime
 * @version 1.0
 * @copyright shinsoft
 */
public class StaxParse {

	public static XMLStreamReader getStreamReader(String content) {
		XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
		try {
			XMLStreamReader reader = xmlInputFactory
					.createXMLStreamReader(new StringReader(content));
			return reader;
		} catch (XMLStreamException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 按节点名，取其值 如取send值"/smil/type/send"
	 * <smil><type><ip>127.0.0.1</ip><send>MmsManagerForSingle
	 * </send></type></smil>
	 * 
	 * @param xmlSource
	 *            String
	 * @param elementName
	 *            String
	 * @return String
	 */
	// 有多个相同节点时不能用这个方法
	public static String getElementTitle(String xmlSource, String elementName) {
		String elementValue = "";
		XMLStreamReader reader = getStreamReader(xmlSource);
		try {
			while (reader.hasNext()) {
				int event = reader.next();
				if (event == XMLStreamConstants.START_ELEMENT) {
					if (elementName.equalsIgnoreCase(reader.getLocalName())) {
						elementValue = reader.getElementText();
						break;
					}
				}

			}
		} catch (XMLStreamException e) {
			e.printStackTrace();
		}
		return elementValue;
	}

	public static void main(String[] args) {
		long start = System.currentTimeMillis();
		String source = "<smil><world/><type><hello/><ip text='hi'>127.0.0.1</ip><send>MmsManagerForSingle</send></type></smil>";
		// String source = "<item></item>";
		// String source = "<item/>";
		String elementName = "ip";
		System.out.println(getElementTitle(source, elementName));
		System.out.println("------------------------------------------");
		System.out.println(System.currentTimeMillis() - start);
	}

	/**
	 * 查询元素节点 如取send值"/smil/type/send"
	 * <smil><type><ip>127.0.0.1</ip><send>MmsManagerForSingle
	 * </send></type></smil>
	 * 
	 * @param xmlSource
	 *            String
	 * @param elementName
	 *            String
	 * @return boolean
	 */
	public static boolean selectElement(String xmlSource, String elementName) {
		boolean b = false;
		XMLStreamReader reader = getStreamReader(xmlSource);
		try {
			while (reader.hasNext()) {
				int event = reader.next();
				if (event == XMLStreamConstants.START_ELEMENT) {
					if (elementName.equalsIgnoreCase(reader.getLocalName())) {
						b = true;
						break;
					}
				}
			}
		} catch (XMLStreamException e) {
			e.printStackTrace();
		}
		return b;
	}

	/**
	 * 这方法有问题
	 * 
	 * 按节点名，取节点属性值 如取send值"/smil/type/send"
	 * <smil><type><ip>127.0.0.1</ip><send>MmsManagerForSingle
	 * </send></type></smil>
	 * 
	 * @param xmlSource
	 *            String
	 * @param elementName
	 *            String
	 * @return String
	 */
	// 只适合单节点，单属性
	public static String getElementProperty(String xmlSource, String elementName) {
		String elementPropertyValue = "";
		XMLStreamReader reader = getStreamReader(xmlSource);
		try {
			while (reader.hasNext()) {
				int event = reader.next();
				if (event == XMLStreamConstants.START_ELEMENT) {
					if (elementName.equalsIgnoreCase(reader.getLocalName())) {
						elementPropertyValue = reader.getAttributeValue(0);
						break;
					}
				}

			}
		} catch (XMLStreamException e) {
			e.printStackTrace();
		}
		return elementPropertyValue;
	}

	public static boolean needConvert(String xmlSource) {
		boolean b = true;
		XMLStreamReader reader = getStreamReader(xmlSource);
		try {
			while (reader.hasNext()) {
				int event = reader.next();
				if (event == XMLStreamConstants.START_ELEMENT
						&& reader.next() == 2) {
					b = false;
					break;
				}
			}
		} catch (XMLStreamException e) {
			e.printStackTrace();
		}

		return b;
	}

}
